package com.orhon.smartcampus.modules.teacher.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.teacher.dao.GetFundingDao;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.GetFunding;
import com.orhon.smartcampus.modules.teacher.gqlInputModel.GQLGetFundingInput;
import com.orhon.smartcampus.modules.teacher.inputMapper.GetFundingInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddGetFundingMutation implements DataFetcher {

    @Autowired
    GetFundingDao getFundingDao;

    @Autowired
    TInformationDao tInformationDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    /**
     * 添加获得资助情况
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLGetFundingInput input = GetFundingInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        GetFunding getFunding = null;

        if (input.getId() != null) {
            getFunding = getFundingDao.get(GetFunding.class, input.getId());
            getFunding.setUpdatedAt(new Date());
        } else {
            getFunding = new GetFunding();
            getFunding.setCreatedAt(new Date());
        }
        if(schoolId != null){

            getFunding.setSchool_id(schoolId);
        }
        if(input.getTeacher_id() != null){

            getFunding.setTeacher_id(input.getTeacher_id());
        }
        if(input.getFundyear() != null){

            getFunding.setFundyear(input.getFundyear());
        }
        if(input.getTime() != null){

            getFunding.setTime(input.getTime());
        }
        if(input.getProjectmoney() != null){

            getFunding.setProjectmoney(input.getProjectmoney());
        }
        if(input.getImage() != null){

            getFunding.setImage(input.getImage());
        }
        if(input.getShenhe() != null){

            getFunding.setShenhe(input.getShenhe());
        }
        if(input.getName() != null){

            getFunding.setName(JSON.toJSONString(input.getName()));
        }
        if(input.getFundunit() != null){

            getFunding.setFundunit(JSON.toJSONString(input.getFundunit()));
        }
        if(input.getFundcontent() != null){

            getFunding.setFundcontent(JSON.toJSONString(input.getFundcontent()));
        }
        try{
            getFundingDao.saveOrUpdate(getFunding);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

